<template>
  <div class="finance-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>财务记录管理</span>
        </div>
      </template>
      
      <el-table :data="records" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="user_id" label="用户ID" />
        <el-table-column prop="order_id" label="订单ID" />
        <el-table-column prop="type" label="类型" />
        <el-table-column prop="amount" label="金额">
          <template #default="scope">
            ¥{{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 记录详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="财务记录详情" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="ID">{{ detailData.id }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ detailData.user_id }}</el-descriptions-item>
        <el-descriptions-item label="订单ID">{{ detailData.order_id }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ detailData.type }}</el-descriptions-item>
        <el-descriptions-item label="金额">¥{{ detailData.amount }}</el-descriptions-item>
        <el-descriptions-item label="描述">{{ detailData.description }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detailData.status === 1 ? '启用' : '禁用' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailData.created_at }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getFinancialRecords, getFinancialRecordById } from '@/api/financialRecord'

// 财务记录数据
const records = ref([])
const loading = ref(false)

// 分页信息
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

// 详情对话框
const detailDialogVisible = ref(false)
const detailData = ref({})

// 获取财务记录列表
const fetchRecords = async () => {
  loading.value = true
  try {
    const res: any = await getFinancialRecords({
      page: pagination.page,
      page_size: pagination.pageSize
    })
    
    if (res.success) {
      records.value = res.data
      pagination.total = res.pagination.total
    } else {
      ElMessage.error(res.message || '获取财务记录列表失败')
    }
  } catch (error) {
    ElMessage.error('获取财务记录列表失败')
  } finally {
    loading.value = false
  }
}

// 处理页面大小变化
const handleSizeChange = (val: number) => {
  pagination.pageSize = val
  fetchRecords()
}

// 处理当前页变化
const handleCurrentChange = (val: number) => {
  pagination.page = val
  fetchRecords()
}

// 查看详情
const handleView = async (row: any) => {
  try {
    const res: any = await getFinancialRecordById(row.id)
    if (res.success) {
      detailData.value = res.data
      detailDialogVisible.value = true
    } else {
      ElMessage.error(res.message || '获取财务记录详情失败')
    }
  } catch (error) {
    ElMessage.error('获取财务记录详情失败')
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchRecords()
})
</script>

<style scoped>
.finance-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>